package com.orangelife.mobile.main.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.bigkoo.convenientbanner.CBViewHolderCreator;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.orangelife.mobile.R;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.orangelife.mobile.main.biz.MainHttpBz;
import com.orangelife.mobile.main.biz.NetworkImageHolderView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 轮播广告页
 * <p/>
 * Created by cx on 2015/12/3.
 *
 * @author cx
 */
public class BannerFragment extends Fragment implements AdapterView.OnItemClickListener {

    private ConvenientBanner convenientBanner;//顶部广告栏控件
    private ArrayList<String> transformerList = new ArrayList<>();
    private View homePageView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (homePageView == null) {
            homePageView = inflater.inflate(R.layout.fragment_banner,
                    container, false);
            initViews();
            requestAdList();
        }
        ViewGroup parent = (ViewGroup) homePageView.getParent();
        if (parent != null) {
            parent.removeView(homePageView);
        }
        return homePageView;
    }

    //点击切换效果
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        String name = transformerList.get(position);
        ConvenientBanner.Transformer transformer = ConvenientBanner.Transformer.valueOf(name);
        convenientBanner.setPageTransformer(transformer);
    }


    private void initViews() {
        convenientBanner = (ConvenientBanner) homePageView.findViewById(R.id.convenientBanner);
    }

    //初始化网络图片缓存库
    private void initImageLoader() {
        //网络图片例子,结合常用的图片缓存库UIL,你可以根据自己需求自己换其他网络图片库
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().
                showImageForEmptyUri(R.drawable.default_banner)
                .cacheInMemory(true).cacheOnDisk(true).displayer(new RoundedBitmapDisplayer(10)).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getActivity().getApplicationContext()).defaultDisplayImageOptions(defaultOptions)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);
    }

    public void requestAdList() {
        MainHttpBz.getAdList(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject jsonObject, int i) {
                List<Map<String, Object>> mapList = MainHttpBz.handleAdList(jsonObject);
                List<String> imageList = new ArrayList<>();
                if (mapList != null) {
                    NetworkImageHolderView.setListMap(mapList);
                    for (final Map<String, Object> advert : mapList) {
                        imageList.add(advert.get("pics").toString());
                    }
                }
                convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
                    @Override
                    public NetworkImageHolderView createHolder() {
                        return new NetworkImageHolderView();
                    }
                }, imageList);

                if (imageList.size() > 1) {
                    //设置翻页的效果，不需要翻页效果可用不设
                    convenientBanner.setPageTransformer(ConvenientBanner.Transformer.DefaultTransformer);
                    //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                    convenientBanner.setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused});
                    convenientBanner.startTurning(5000);
                }
            }
        });
    }


    // 停止自动翻页
    @Override
    public void onPause() {
        super.onPause();
        //停止翻页
        convenientBanner.stopTurning();
    }
}
